package cs419.magnusmi.trackitcs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.json.JSONException;

/**
 * Created by Adam on 2/11/14.
 */
public class AddUser extends Activity {

    private ProgressDialog pDialog;

    private static String register = "http://web.engr.oregonstate.edu/~cohenad/cs419/index.php/register";
    JSONParser jsonParser = new JSONParser();
    String uname;
    String pass;
    String fname;
    String lname;
    String email;


    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        Intent i = getIntent();

        uname = i.getStringExtra("user");
        pass = i.getStringExtra("pass");
        fname = i.getStringExtra("fname");
        lname = i.getStringExtra("lname");
        email = i.getStringExtra("email");
        //debug print statement System.out.printf("%s, %s, %s, %s, %s", uname, pass, fname, lname, email);

        new RegisterUser().execute();


        }

        class RegisterUser extends AsyncTask<String, String, String>{

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(AddUser.this);
                pDialog.setMessage("Attempting to add user");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }


            protected String doInBackground(String... args){
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("user", uname));
            params.add(new BasicNameValuePair("pass", pass));
            params.add(new BasicNameValuePair("fname", fname));
            params.add(new BasicNameValuePair("lname", lname));
            params.add(new BasicNameValuePair("email", email));

            JSONObject json = jsonParser.makeHttpRequest(register, "POST", params);

            String response = "";
            Boolean errorState = true;
            try{
                response = json.getString("message");
                errorState = json.getBoolean("error");
                if (!errorState){
                    Intent i = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(i);
                    finish();
                //test2 = json.getString("error");
                }

            }
            catch (JSONException e) {
                System.out.println("Invalid key");
            }

            System.out.println(response);
            return null;


        }

        protected void onPostExecute(String file_url) {
           // dismiss the dialog once done
            pDialog.dismiss();
            }
    }





}
